package org.example.intern.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.intern.entity.common.SysUser;
import org.example.intern.entity.dto.req.LoginFormReq;
import org.example.intern.service.ISysUserService;
import org.example.intern.utils.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * @author: world
 * @date: 2022/4/18 15:34
 * @description: 用户相关接口
 */

@Api(tags = "用户接口")
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private ISysUserService userService;

    @ApiOperation("用户登录接口")
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Rest<SysUser> login(@Validated @RequestBody LoginFormReq req){
        final SysUser login = userService.login(req.getUsername(), req.getPassword());
        login.setRoles(Collections.singletonList("admin"));
        return Rest.success(login);
    }

    @ApiOperation("通过token(用户名)获取用户信息")
    @RequestMapping(value = "info",method = RequestMethod.GET)
    public Rest<SysUser> info(@RequestParam(value = "token") String username){
        final SysUser byUsername = userService.findByUsername(username);
        byUsername.setRoles(Collections.singletonList("admin"));
        return Rest.success(byUsername);
    }

    @ApiOperation("用户注销")
    @RequestMapping(value = "logout",method = RequestMethod.POST)
    public Rest<Boolean> logout(){
        return Rest.success(true);
    }
}
